package com.make.sunflower.advice;

import com.make.sunflower.common.Result;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class CommonResultAdvice {

    @Pointcut("@annotation(com.make.sunflower.advice.annotation.CommonResult)")
    public void service(){};

    @Around("service()")
    public Result afterReturning(ProceedingJoinPoint point) throws Throwable {
        Result result ;
        try{
            Object proceed = point.proceed();
            if (proceed instanceof Result){
                result =  (Result)proceed;
            }else {
                result = Result.success(proceed);
            }
        }catch(Exception e){
            result = Result.fail(null);
        }
        return result;
    }
}
